<template>
  <div class="loading-wrapper">
    <div class="login-container" >
      <el-form :model="form" :rules="rules2"
               status-icon
               ref="ruleForm2"
               label-position="left"
               label-width="0px"
               class="demo-ruleForm login-page"
               style="background: rgba(240 , 240 , 240 ,0.7);">
        <p class="title">登录</p>
        <el-form-item prop="phone">
          <el-input type="text"
                    v-model="form.phone"
                    auto-complete="off"
                    placeholder="手机号"
          ></el-input>
        </el-form-item>
        <el-button type="primary" style="width:100%;" @click="SendVerificationCode">{{btntxt}}</el-button>
        <el-form-item prop="code">
          <el-input type="code"
                    v-model="form.code"
                    auto-complete="off"
                    placeholder="验证码"
                    style="margin-top: 20px"
          ></el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" @click="SignIn" :loading="logining">登录</el-button>
          <p class="register" @click="gotoRegister">没有账号？立即注册</p>
          <p class="register" @click="gotouserLogin">用户名登录</p>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>

<script>
  import axios from 'axios'
  import cookie from 'vue-cookie'
  export default {
    data(){
      return {
        disabled:false,
        time:5,
        btntxt:"发送验证码",
        logining: false,
        form: {
          phone: "",
          code: "",
        },
        rules2: {
          phone: [{required: true, message: '请输入手机号', trigger: 'blur'}],
        },
        checked: false
      }
    },
    methods: {
      SendVerificationCode() {
        this.time=60;
        this.timer();
        axios.post("/api/phone/sendphone",{phone:this.form.phone} ).then((res) => {
          console.log(res);
        });
      },
      //发送手机验证码倒计时
      timer() {
        if (this.time > 0) {
          this.disabled = true;
          this.time--;
          this.btntxt = this.time + "秒后可重新获取";
          setTimeout(this.timer, 1000);
        } else {
          this.time = 0;
          this.btntxt = "发送验证码";
          this.disabled = false;
        }
      }
      ,
      SignIn() {
        axios.post("/api/user/findphone",this.form).then((res) => {
          if (res.data.code==200){
            cookie.set("token",res.data.data);
            sessionStorage.setItem('user',res.data.mgs);
            alert("登录成功")
            this.$router.push({path: '/'});
          }else{
            alert("登录失败");

          }
        });
      },
      gotoRegister() {
        this.$router.push({
          path: "/register"
        });
      },
      gotouserLogin(){
        this.$router.push({
          path: "/login"
        });
      },
    }
  };
</script>

<style>
  .loading-wrapper {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    background-image: url("http://qom05wwz5.hd-bkt.clouddn.com/bg.jpg");
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .register {
    margin-top: 10px;
    font-size: 14px;
    line-height: 22px;
    color: #1ab2ff;
    cursor: pointer;
    text-align: left;
    text-indent: 8px;
    width: 160px;
  }
  .login-container {
    width: 100%;
    height: 100%;

  }
  .title {
    font-size: 26px;
    line-height: 50px;
    font-weight: bold;
    margin: 10px;
    text-align: center;
  }
  .login-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
</style>

<!--
<template>
  <div>
    <p>
      手机号：<el-input v-model="form.phone"></el-input>
      <el-button type="primary" round @click="SendVerificationCode"
      >发送验证码</el-button
      >
    </p>
    <p>验证码：<el-input v-model="form.code"></el-input></p>
    <p><el-button type="info" round @click="SignIn">信息按钮</el-button></p>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    name: "phoneLogin",
    data() {
      return {
        form: {
          phone: "",
          code: "",
        },
      };
    },
    methods: {
      SendVerificationCode() {
        axios.post("/api/phone/sendphone",{phone:this.form.phone} ).then((res) => {
          console.log(res);
        });
      },
      SignIn() {
        axios.post("/api/user/findphone",{phone:this.form.phone,code:this.from.code} ).then((res) => {
          if (res.data.code==200){
            alert(res.data.mgs);
            cookie.set("token",res.data.data);
            sessionStorage.setItem('user', this.user.name);
            this.$router.push({path: '/'});
          }else{
            alert(res.data.mgs);
          }
        });
      },
    },
  };
</script>
<style scoped>
</style>
-->
